package cn.edu.jju.stepcounter2.main.presenter;


import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import cn.edu.jju.stepcounter2.R;
import cn.edu.jju.stepcounter2.adapter.DayDataAdapter;
import cn.edu.jju.stepcounter2.bean.DayData;
import cn.edu.jju.stepcounter2.main.model.MainModel;
import cn.edu.jju.stepcounter2.main.model.MainModelImpl;
import cn.edu.jju.stepcounter2.main.model.OnDataGetListener;
import cn.edu.jju.stepcounter2.main.view.MainView;
import cn.edu.jju.stepcounter2.utils.GetDate;
import cn.edu.jju.stepcounter2.view.CircleView;

import static android.util.Log.i;

/**
 * Created by roor on 2016/12/08
 */

public class MainPresenterImpl implements MainPresenter {
    private MainModel mainModel;
    private MainView mainView;
    private List<DayData> dayDatas = new ArrayList<>();
    private List<View> vList = new ArrayList<>();
    private DayDataAdapter adapter;
    private ViewPager viewpager;

    public MainPresenterImpl(MainView mainView) {
        this.mainView = mainView;
        mainModel = new MainModelImpl();
    }

    @Override
    public void setViewPager() {
        viewpager = mainView.getViewpager();

        mainModel.getData(new OnDataGetListener() {
            @Override
            public void onDataGetSuccess(List<DayData> dayDataList) {
                dayDatas.clear();
                dayDatas = dayDataList;

            }

            @Override
            public void onDataGetFailed(String error) {

            }
        });

        for (DayData d : dayDatas) {
            View v = mainView.getViewPgerContent();
            TextView tv_date = (TextView) v.findViewById(R.id.tv_date);
//            TextView tv_steps = (TextView) v.findViewById(R.id.tv_steps);
//            TextView tv_expectStep = (TextView) v.findViewById(R.id.tv_expectStep);
            TextView tv_calConsume = (TextView) v.findViewById(R.id.tv_calConsume);
            CircleView cv_main = (CircleView) v.findViewById(R.id.cv_main);
            showInMainCircle(cv_main, d.getSteps(), d.getExpectStep());
            if (d.getDate().equals(GetDate.get())) {
                tv_date.setText("今天");
            } else {

                tv_date.setText(d.getDate());
            }
//            tv_steps.setText(d.getSteps());
//            tv_expectStep.setText(d.getExpectStep());
            DecimalFormat decimalFormat = new DecimalFormat("0.00");

            String calConsume = decimalFormat.format(Double.parseDouble(d.getCalConsume())/1000);
            tv_calConsume.setText(calConsume);
            vList.add(v);
            v.setTag(d.getDate());
            i("@@@", "tag" + v.getTag());

        }
        adapter = new DayDataAdapter(vList);
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(vList.size() - 1);
        viewpager.addOnPageChangeListener(myVPChangeListener);

    }

    private ViewPager.OnPageChangeListener myVPChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            CircleView circleView = (CircleView) vList.get(position).findViewById(R.id.cv_main);
            i("aaa", "onPageSelected" + circleView.getSteps());
            circleView.startCustomAnimation();


        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    private void showInMainCircle(CircleView cv_main, String steps, String expectStep) {
        cv_main.setExpectStep(Integer.parseInt(expectStep));
        cv_main.setTotalStep(Integer.parseInt(steps));
        cv_main.setSteps(Integer.parseInt(steps));

        cv_main.setProgress((float) (Integer.parseInt(steps) / (Integer.parseInt(expectStep) * 1.0)) * 360);
        cv_main.invalidate();

    }

    @Override
    public void nortifyVPChange() {
        viewpager = mainView.getViewpager();
        mainModel.getData(new OnDataGetListener() {
            @Override
            public void onDataGetSuccess(List<DayData> dayDataList) {
                dayDatas.clear();
                dayDatas = dayDataList;
            }

            @Override
            public void onDataGetFailed(String error) {

            }
        });
        DayData d = null;
        for (int i = 0; i < dayDatas.size(); i++) {
            if (dayDatas.get(i).getDate().equals(GetDate.get())) {
                d = dayDatas.get(i);
                i("@@@@", "nortifyVPChange" + dayDatas.get(i).getSteps());
            }
        }

        View test = viewpager.findViewWithTag(d.getDate());
//        TextView viewById = (TextView) test.findViewById(R.id.tv_steps);
        TextView view = (TextView) test.findViewById(R.id.tv_calConsume);
//        viewById.setText(d.getSteps());
        i("@@@", "nortifyVPChange" + d.getSteps());
        DecimalFormat decimalFormat = new DecimalFormat("0.00");

        String calConsume = decimalFormat.format(Double.parseDouble(d.getCalConsume())/1000);
        view.setText(calConsume);
        CircleView cv_main = (CircleView) test.findViewById(R.id.cv_main);
        showInMainCircle(cv_main, d.getSteps(), d.getExpectStep());
//        TextView tv_expectStep = (TextView) test.findViewById(R.id.tv_expectStep);
//        tv_expectStep.setText(d.getExpectStep());


    }

    @Override
    public void newVpContent() {
        viewpager = mainView.getViewpager();
        mainModel.getData(new OnDataGetListener() {
            @Override
            public void onDataGetSuccess(List<DayData> dayDataList) {
                dayDatas.clear();
                dayDatas = dayDataList;
            }

            @Override
            public void onDataGetFailed(String error) {

            }
        });
        DayData d = null;
        for (int i = 0; i < dayDatas.size(); i++) {
            if (dayDatas.get(i).getDate().equals(GetDate.get())) {
                d = dayDatas.get(i);
            }
        }

        View v = mainView.getViewPgerContent();
        TextView tv_date = (TextView) v.findViewById(R.id.tv_date);
//        TextView tv_steps = (TextView) v.findViewById(R.id.tv_steps);
//        TextView tv_expectStep = (TextView) v.findViewById(R.id.tv_expectStep);
        TextView tv_calConsume = (TextView) v.findViewById(R.id.tv_calConsume);
        if (d.getDate().equals(GetDate.get())) {
            tv_date.setText("今天");
        } else {

            tv_date.setText(d.getDate());
        }
//        tv_steps.setText(d.getSteps());
//        tv_expectStep.setText(d.getExpectStep());
        DecimalFormat decimalFormat = new DecimalFormat("0.00");

        String calConsume = decimalFormat.format(Double.parseDouble(d.getCalConsume())/1000);
        tv_calConsume.setText(calConsume);
        v.setTag(d.getDate());
        CircleView cv_main = (CircleView) v.findViewById(R.id.cv_main);
        showInMainCircle(cv_main, d.getSteps(), d.getExpectStep());
        vList.add(v);

        adapter.notifyDataSetChanged();
        viewpager.setCurrentItem(vList.size() - 1);


    }

    @Override
    public void notifyVPAll() {
        viewpager = mainView.getViewpager();
        mainModel.getData(new OnDataGetListener() {
            @Override
            public void onDataGetSuccess(List<DayData> dayDataList) {
                dayDatas.clear();
                dayDatas = dayDataList;
            }

            @Override
            public void onDataGetFailed(String error) {

            }
        });
        vList.clear();
        for (DayData d : dayDatas) {
            View v = mainView.getViewPgerContent();
            TextView tv_date = (TextView) v.findViewById(R.id.tv_date);
//            TextView tv_steps = (TextView) v.findViewById(R.id.tv_steps);
//            TextView tv_expectStep = (TextView) v.findViewById(R.id.tv_expectStep);
            TextView tv_calConsume = (TextView) v.findViewById(R.id.tv_calConsume);
            if (d.getDate().equals(GetDate.get())) {
                tv_date.setText("今天");
            } else {

                tv_date.setText(d.getDate());
            }
//            tv_steps.setText(d.getSteps());
//            tv_expectStep.setText(d.getExpectStep());
            DecimalFormat decimalFormat = new DecimalFormat("0.00");

            String calConsume = decimalFormat.format(Double.parseDouble(d.getCalConsume())/1000);
            tv_calConsume.setText(calConsume);
            CircleView cv_main = (CircleView) v.findViewById(R.id.cv_main);
            showInMainCircle(cv_main, d.getSteps(), d.getExpectStep());

            v.setTag(d.getDate());
            vList.add(v);

        }
        adapter.notifyDataSetChanged();
        viewpager.setCurrentItem(vList.size() - 1);
        //viewpager.addOnPageChangeListener(myVPChangeListener);

    }


}